﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mukhopatro.Helpers;
using Mukhopatro.BusinessLogicLayer;
using Mukhopatro.BusinessEntities;

namespace Mukhopatro.Admin
{
    public partial class news : System.Web.UI.Page
    {
        public String _SORTBY
        {
            get
            {
                return ViewState["_SORTBY"].ToString();
            }
            set
            {
                ViewState["_SORTBY"] = value;
            }
        }

        public Boolean _IS_ASC
        {
            get
            {
                return Convert.ToBoolean(ViewState["_IS_ASC"].ToString());
            }
            set
            {
                ViewState["_IS_ASC"] = value;
            }
        }

        public Boolean _IS_SEARCH
        {
            get
            {
                return Convert.ToBoolean(ViewState["_IS_SEARCH"].ToString());
            }
            set
            {
                ViewState["_IS_SEARCH"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _IS_SEARCH = false;
                _IS_ASC = true;
                _SORTBY = "ContentId";
                LoadData();
            }
        }

        private void LoadData()
        {
            BindGrid(_IS_SEARCH, _SORTBY, 0, SiteSettings.PageSize);
            BindPager(_IS_SEARCH, 0, SiteSettings.PageSize);

        }

        protected void repPager_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            BindGrid(_IS_SEARCH, _SORTBY, Convert.ToInt32(e.CommandArgument) - 1, SiteSettings.PageSize);
        }

        private void BindPager(bool isSelectAll, int currentPageIndex, int pageSize)
        {
            CustomerNewsContentBLL bll = new CustomerNewsContentBLL();
            Int32 totalCount = 0;
            Int32 pageCount = 0;
            totalCount = bll.GetNewsContentsTotalCount(obj => obj.ClientId == SessionManager.CustomerId);
            pageCount = Convert.ToInt32(totalCount / pageSize);
            litTotalRecordCount.Text = totalCount.ToString();
            repPager.DataSource = Enumerable.Range(1, pageCount).ToList();
            repPager.DataBind();
        }

        private void BindGrid(bool isSelectAll, string orderBy, int currentPageIndex, int pageSize)
        {
            CustomerNewsContentBLL bll = new CustomerNewsContentBLL();
            List<CustomerNewsContent> contents;
            string sortDir = _IS_ASC ? "asc" : "desc";
            contents = bll.GetNewsContents(obj => obj.ClientId == SessionManager.CustomerId, orderBy + " " + sortDir, currentPageIndex, pageSize);



            grvData.DataSource = contents;
            grvData.DataBind();


            //grvConsumerList.DataSource = members;
            //grvConsumerList.DataBind();
        }
    }
}